package com.qqt.csr.im.entity.msg;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.qqt.csr.common.enums.DeletedEnum;
import com.qqt.csr.common.sensitiveword.SensitiveWordMsg;
import com.qqt.csr.common.utils.JsonUtil;
import com.qqt.csr.im.dto.MsgAccountDTO;
import com.qqt.csr.im.enums.MsgFlowTypeEnum;
import com.qqt.csr.im.enums.MsgTypeEnum;
import com.qqt.csr.im.enums.ReadFlagEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

/**
 * @Author: yang hao
 * @Date: 2024/10/14 10:02
 * @Description: 消息
 **/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "消息")
@Accessors(chain = true)
@Document(collection = "im_message")
public class Message implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    @Id
    @Schema(description = "消息id")
    private String id;

    @Indexed(name = "idx_seq")
    @Schema(description = "消息序号")
    private String seq;

    @Schema(description = "发送方")
    private MsgAccountDTO sender;

    @Schema(description = "接收方")
    private MsgAccountDTO recipient;

    @Indexed(name = "idx_conId")
    @Schema(description = "会话id")
    private String conversationId;

    @Schema(description = "消息内容")
    private Object content;

    @Schema(description = "消息类型")
    private MsgTypeEnum type;

    @Schema(description = "消息流向")
    private MsgFlowTypeEnum flow;

    @Schema(description = "扩展信息")
    private String artificialExt;

    @Schema(description = "是否已读，0：未读，1：已读")
    private ReadFlagEnum readFlag;

    @Schema(description = "是否删除，0：未删除，1：已删除")
    private DeletedEnum deleted;

    @Schema(description = "创建时间")
    private String createTime;

    @Schema(description = "更新时间")
    private String updateTime;

    @Schema(description = "房间id")
    private String roomId;

    @Schema(description = "租户id")
    private String tenantId;

    @Schema(description = "命中的敏感词位置")
    private List<SensitiveWordMsg> sensitiveWords;

    /**
     * 转换消息内容类型
     *
     * @return
     */
    @JsonIgnore
    public void convertContent() {
        if(this.content == null){
            if (MsgTypeEnum.SystemElem.equals(this.type) || MsgTypeEnum.TextElem.equals(this.type)) {
                this.content = new TextContent();
            } else if (MsgTypeEnum.ImageElem.equals(this.type)) {
                this.content = new ImageContent();
            } else {
                this.content = new FileContent();
            }
            return;
        }else if(this.content instanceof Content){
            return;
        }

        if (MsgTypeEnum.SystemElem.equals(this.type) || MsgTypeEnum.TextElem.equals(this.type)) {
            this.content = JsonUtil.toObject(JsonUtil.toJson(this.content), TextContent.class);
        } else if (MsgTypeEnum.ImageElem.equals(this.type)) {
            this.content = JsonUtil.toObject(JsonUtil.toJson(this.content), ImageContent.class);
        } else {
            this.content = JsonUtil.toObject(JsonUtil.toJson(this.content), FileContent.class);
        }
    }

}
